//
//  AddAddressViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/19.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "AddAddressViewController.h"
#import "ZHFAddTitleAddressView.h"
#import "AddAddressParametersModel.h"
#import "AddAddressStore.h"
#import "AddressModel.h"

@interface AddAddressViewController ()<ZHFAddTitleAddressViewDelegate>
{
    
    __weak IBOutlet UITextField *peopleTf;
    __weak IBOutlet UITextField *phoneTf;
    __weak IBOutlet UITextField *localTf;
    __weak IBOutlet UITextField *detalLocalTf;
    
    AddAddressParametersModel *parametersModel;

}

@property(nonatomic,strong)ZHFAddTitleAddressView * addTitleAddressView;


@end

@implementation AddAddressViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavBarWithTitle:@"添加地址"];
    
    phoneTf.keyboardType = UIKeyboardTypeNumberPad;
    
    parametersModel = [[AddAddressParametersModel alloc] init];
    
    
    [self setUI];
}

-(void)setUI{
    
    self.addTitleAddressView = [[ZHFAddTitleAddressView alloc]init];
    self.addTitleAddressView.title = @"选择地址";
    self.addTitleAddressView.userID = 7;
    self.addTitleAddressView.delegate1 = self;
    self.addTitleAddressView.defaultHeight = 350;
    self.addTitleAddressView.titleScrollViewH = 37;
    [self.view addSubview:[self.addTitleAddressView initAddressView]];
    
}


- (IBAction)onSelectionAd:(UIButton *)sender {
    
    [self.view endEditing:YES];
    [self.addTitleAddressView addAnimate];
}


-(void)cancelBtnClick:(NSString *)titleAddress titleID:(NSString *)titleID{
    
    localTf.text = titleAddress;
    
    parametersModel.region = titleID;
    NSLog( @"%@", [NSString stringWithFormat:@"打印的对应省市县的id=%@",titleID]);
}



- (IBAction)onPostBtn:(id)sender {

    [self.view endEditing:YES];
    
    
    if (peopleTf.text.length <= 0) {

        return [self showMBPError:@"请输入收货人"];
    }

    if (phoneTf.text.length <= 0) {

        return [self showMBPError:@"请输入联系电话"];
    }

    if (![phoneTf.text isVaildMobileNo]) {

        return [self showMBPError:@"请输入正确手机号"];
    }

    if (localTf.text.length <= 0) {

        return [self showMBPError:@"请输入所在区域"];
    }

    if (detalLocalTf.text.length <= 0) {

        return [self showMBPError:@"请输入详细地址"];
    }

    parametersModel.customId = [UserDefaultsManager sharedInstance].user_id;
    parametersModel.name = peopleTf.text;
    parametersModel.phone = phoneTf.text;
    parametersModel.address = detalLocalTf.text;

    AddAddressStore *store = [[AddAddressStore alloc] init];

    
    MJWeakSelf
    [store addAdresstWithParameters:parametersModel WithSuccess:^{
        
        [weakSelf showMBPError:@"添加成功"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"AddAdressSuccess" object:nil];
        
        //GCD延迟
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        });
        
        
    } failure:^(NSError * error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
    
}


@end
